// 引入其他方法
import {
    createRouter, //创建路由
    createWebHashHistory //#（url）模式
} from 'vue-router'

import  Index  from '~/pages/index.vue';
import Login from '~/pages/login.vue';
import NotFound from '~/pages/404.vue';
import Admin from '~/layouts/admin.vue';
import GoodList from '~/pages/goods/list.vue';
import CategoryList from '~/pages/category/list.vue';
import UserList from '~/pages/user/list.vue';
import OrderList from '~/pages/order/list.vue';
import CommentList from '~/pages/comment/list.vue';
import ImageList from '~/pages/image/list.vue';
import NoticeList from '~/pages/notice/list.vue';
import SettingList from '~/pages/setting/list.vue';
import CouponList from '~/pages/coupon/list.vue';
import ManagerList from '~/pages/manager/list.vue';
import AccessList from '~/pages/access/list.vue';
import RoleList from '~/pages/role/list.vue';
import SkusList from '~/pages/skus/list.vue';
import LevelList from '~/pages/level/list.vue';
import SettingBuy from '~/pages/setting/buy.vue'
import SettingShip from '~/pages/setting/ship.vue'
import DistributionIndex from '~/pages/distribution/index.vue'
import DistributionSetting from '~/pages/distribution/setting.vue';



const routes = [{
    path: '/',
    name:'admin',
    component: Admin,

},
//路由错了就会出现404错误
{
    path: "/login",
    component: Login,
    meta: {
        title: "登录页"
    }
},
// 从官网上粘贴:捕获所有路由和404...
{
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound
},
]; //路由配置

//动态路由，用于配置菜单动态添加路由
const asyncRoutes = [{
    //指定后台首页的路径(直接访问首页是访问不到的因位属于根目录)
    path: '/',
    name:'/',
    //指定他的主键（页面）
    component: Index,
    meta: {
        title: '后台首页'
    },
},
{
    path: '/goods/list',
    name: '/goods/list',
    component: GoodList,
    meta: {
        title: '商品管理'
    }
},
{
    path: '/category/list',
    name: '/category/list',
    component: CategoryList,
    meta: {
        title: '分类列表'
    }
},
{
    path: '/user/list',
    name: '/user/list',
    component: UserList,
    meta: {
        title: '用户列表'
    }
},
{
    path: '/order/list',
    name: '/order/list',
    component: OrderList,
    meta: {
        title: '订单列表'
    }
},
{
    path: '/comment/list',
    name: '/comment/list',
    component: CommentList,
    meta: {
        title: '评价列表'
    }
},
{
    path: '/image/list',
    name: '/image/list',
    component: ImageList,
    meta: {
        title: '图库列表'
    }
},
{
    path: '/notice/list',
    name: '/notice/list',
    component: NoticeList,
    meta: {
        title: '公告列表'
    }
},
{
    path: '/setting/base',
    name: '/setting/base',
    component: SettingList,
    meta: {
        title: '配置列表'
    }
},
{
    path: '/coupon/list',
    name: '/coupon/list',
    component: CouponList,
    meta: {
        title: '优惠券列表'
    }
},

{
    path: '/manager/list',
    name: '/manager/list',
    component: ManagerList,
    meta: {
        title: '管理员管理'
    }
},
{
    path: '/access/list',
    name: '/access/list',
    component: AccessList,
    meta: {
        title: '菜单权限管理'
    }
},
{
    path: '/role/list',
    name: '/role/list',
    component: RoleList,
    meta: {
        title: '角色权限管理'
    }
},
{
    path: '/skus/list',
    name: '/skus/list',
    component: SkusList,
    meta: {
        title: '规格管理'
    }
},
{
    path: '/level/list',
    name: 'level/list',
    component: LevelList,
    meta: {
        title: '会员等级'
    }
},
{
    path: '/setting/buy',
    name: '/setting/buy',
    component: SettingBuy,
    meta: {
        title: '支付配置'
    }
},
{
    path: '/setting/ship',
    name: '/setting/ship',
    component: SettingShip,
    meta: {
        title: '物流设置'
    }
},
{
    path: '/distribution/index',
    name: '/distribution/index',
    component: DistributionIndex,
    meta: {
        title: '分销首页'
    }
}
,
{
    path: '/distribution/setting',
    name: '/distribution/setting',
    component: DistributionSetting,
    meta: {
        title: '分销设置'
    }
}

]

// 新添加的页面没有导出去
export const router = createRouter({
    // 官方文档要求参数名必须是history,routes不能擅自更改
    history:createWebHashHistory(),
    // key和value值一样可以省略 写成 routers
    routes:routes   
})

//定义动态添加路由的法方
export function addRoutes(menus){
    //是否有新的路由
    let hasNewRoutes = false
    //递归获取子组件
    const findAndADDRoutesByMenus = (arr) =>{
        //默认产生的forEach有问题
       arr.forEach(e => {
        let item = asyncRoutes.find(o=>o.path == e.frontpath)
        //检查路由是否存在，不存在就创建路由
        if(item && !router.hasRoute(item.path)){
            router.addRoute('admin',item)
            hasNewRoutes = true
        }
        //判断有子路由
        if(e.child && e.child.length > 0){
            findAndADDRoutesByMenus(e.child)
        }
       }); 
    }

    findAndADDRoutesByMenus(menus)
    // console.log(router.getRoutes());
    return hasNewRoutes
}